﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL;
using Model;
using MySql.Data.MySqlClient;


namespace DAL
{
    public class userDal
    {
        /// <summary>
        /// 通过用户ID查信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static ms_user Loginbyuid(string uid)
        {
            string sql = "select * from ms_user where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", uid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_user muser = new ms_user();
            if (rd.Read())
            {
                muser.id = rd["id"].ToString();
                muser.username = rd["username"].ToString();
                muser.password = rd["password"].ToString();
                muser.integral = rd["integral"].ToString();
                muser.balance = rd["balance"].ToString();
                muser.nickname = rd["nickname"].ToString();
                muser.withdrawal_amount = rd["withdrawal_amount"].ToString();
                muser.amount = rd["amount"].ToString();
                muser.login_ip = rd["login_ip"].ToString();
                muser.head_portrait = rd["head_portrait"].ToString();
                muser.login_time = rd["login_time"].ToString();
                muser.register_time = rd["register_time"].ToString();
            }
            rd.Close();
            return muser;

        }
        /// <summary>
        /// 通过手机查用户
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static ms_user Querybyphone(string phone)
        {
            string sql = "select * from ms_user where username=@username";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("username",phone);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_user muser = new ms_user();
            if (rd.Read())
            {
                muser.id = rd["id"].ToString();
                muser.username = rd["username"].ToString();
                muser.password = rd["password"].ToString();
                muser.integral = rd["integral"].ToString();
                muser.balance = rd["balance"].ToString();
                muser.nickname = rd["nickname"].ToString();
                muser.withdrawal_amount = rd["withdrawal_amount"].ToString();
                muser.amount = rd["amount"].ToString();
                muser.login_ip = rd["login_ip"].ToString();
                muser.head_portrait = rd["head_portrait"].ToString();
                muser.login_time = rd["login_time"].ToString();
                muser.register_time = rd["register_time"].ToString();
            }
            rd.Close();
            return muser;

        }
        /// <summary>
        /// 用户注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static int Reguser(string username, string pwd)
        {
            string sql = "insert into ms_user(username,password,register_time,nickname) values(@username,@password,@register_time,@nickname)";
            MySqlParameter[] values = new MySqlParameter[4];
            values[0] = new MySqlParameter("username", username);
            values[1] = new MySqlParameter("password", pwd);
            values[2] = new MySqlParameter("register_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            values[3] = new MySqlParameter("nickname", username.Substring(0, 3) + "****" + username.Substring(7, 4));
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 虚假数据详情
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public static ms_user Loginbyuid1(string uid)
        {
            string sql = "select * from ms_user_copy where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", uid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_user muser = new ms_user();
            if (rd.Read())
            {
                muser.id = rd["id"].ToString();
                muser.username = rd["username"].ToString();
                muser.password = rd["password"].ToString();
                muser.integral = rd["integral"].ToString();
                muser.balance = rd["balance"].ToString();
                muser.nickname = rd["nickname"].ToString();
                muser.withdrawal_amount = rd["withdrawal_amount"].ToString();
                muser.amount = rd["amount"].ToString();
                muser.login_ip = rd["login_ip"].ToString();
                muser.head_portrait = rd["head_portrait"].ToString();
                muser.login_time = rd["login_time"].ToString();
                muser.register_time = rd["register_time"].ToString();
            }
            rd.Close();
            return muser;

        }
        /// <summary>
        /// 查用户消费次数
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Querycount(string uid, string merchantid)
        {
            string sql = "select count(*) as count from ms_order where user_id=@user_id and merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("user_id", uid);
            values[1] = new MySqlParameter("merchant_id", merchantid);
            int count = 0;
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            if (rd.Read())
            {
                count = Convert.ToInt32(rd["count"]);
            }
            rd.Close();
            return count;
        }
        /// <summary>
        /// 查用户消费
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static double Querymoney(string uid, string merchantid)
        {
            string sql = "select sum(pay_price) as price from ms_order where user_id=@user_id and merchant_id=@merchant_id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("user_id", uid);
            values[1] = new MySqlParameter("merchant_id", merchantid);
            double money = 0;
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            if (rd.Read())
            {
                if (!string.IsNullOrEmpty(rd["price"].ToString()))
                    money = Convert.ToInt32(rd["price"]);
            }
            rd.Close();
            return money;
        }


    }
}
